import tool.TreeNode;
import tool.TreeNodePrint;
import tool.input;

/**
 * @Description: 左叶子之和
 * 给定二叉树的根节点 root ，返回所有左叶子之和。
 * @Date:Create in 21:58  2022/7/27
 */
public class sumOfLeftLeaves0404 {

    public static void main(String[] args) {
        String s = "[1,2,3,4,5]";
        //从string转换到tree，获取root节点
        TreeNode root = input.getTree(s);
        //打印这个树，但是是转了90度
        TreeNodePrint.printTree(root);
        //输出你的结果
        System.out.println(sumOfLeftLeaves(root));
    }

    //递归方法DFS
    public static int sumOfLeftLeaves(TreeNode root) {
        int sum1 = leftSum(root);
        return sum1;
    }
    public static int leftSum(TreeNode root) {
        if(root==null) return 0;
        int sum =0 ;
        if(root.left!=null && root.left.left==null && root.left.right==null) {
            sum = root.left.val;
        }
        int left = leftSum(root.left);
        int right = leftSum(root.right);
//        System.out.println(sum);
        return left+right+sum;
    }
}
